layui.use(['table','form','jquery'], function() {
    var table = layui.table,
        $ = layui.jquery,
        form = layui.form,
        part = null;


    var tabInstance = table.render({
        elem: '#saleAreaList',
        id:'saleAreaTable',
        url: '/sale-area/list', //数据接口
        page: true, //开启分页
        limit: 5,
        limits:[5,10,15,20],
        request:{
            pageName:'current',
            limitName:'size'
        },
        cols: [[ //表头
            {field: 'saleAreaId', title: '出售区域编号', sort: true, fixed: 'left', align: 'center'},
            {field: 'saleAreaName', title: '区域名', align: 'center'},
            {field:'operation', title: '操作', fixed: 'right', toolbar: "#saleAreaTool", align: 'center'}
        ]],
        done: function () {
            $.post('/check/check-role',function(info) {
                part = info.part;
                console.log(info);
                if(part==3||part==2){
                    $(".add_btn").hide();
                    $("[data-field='operation']").find('#edit_btn').hide();
                    $("[data-field='operation']").find('#edit_btn').attr("class","layui-btn layui-btn-disabled layui-btn-sm layui-icon layui-icon-edit");
                    $("[data-field='operation']").find('#del_btn').hide();
                    $("[data-field='operation']").find('#del_btn').attr("class","layui-btn layui-btn-disabled layui-btn-sm layui-icon layui-icon-delete");
                }
            })
        }
    });

    form.on('submit(search)',function(data){
        console.log(data);
        tabInstance.reload({
            where :data.field
        });
        return false;
    });

    //添加
    $(".add_btn").click(function () {

        layer.open({
            type: 2,
            content: 'views/sale/sale_area_add.jsp',
            skin:'layui-layer-molv',
            title:"添加区域",
            area:["430px","450px"],
            btn:['确定','关闭'],
            yes: function (index,layero) {
                var sale_area_add = layer.getChildFrame('body',index).find('#sale_area_add');
                var sale_area_data = {};
                sale_area_add.serializeArray().forEach(function (val) {
                    sale_area_data[val.name] = val.value;
                    console.log(val.name,val.value);
                });
                if(sale_area_data["saleAreaName"]!=""){
                    $.post('/sale-area/save',sale_area_data,function(info) {
                        console.log(info);
                        layer.close(index);
                        layer.msg(info.msg);
                        tabInstance.reload();
                    })
                }else layer.msg("请输入【区域名】！");
            }
        })
    });

    //编辑、删除
    table.on('tool(saleAreaList)',function(obj){
        console.log(obj);
        switch (obj.event){
            case 'edit_btn':
                //编辑
                layer.open({
                    type: 2,
                    content : 'views/sale/sale_area_edit.jsp',
                    skin: 'layui-layer-molv',
                    title: '出售区域管理',
                    area:["430px","450px"],
                    btn: ['确定','关闭'],
                    success: function (layero,index) {
                        var sale_area_edit = layer.getChildFrame('body', index).find("#sale_area_edit");
                        sale_area_edit.find(".sale_area_name").val(obj.data.saleAreaName);
                        sale_area_edit.find(".sale_area_id").val(obj.data.saleAreaId);
                    },
                    yes: function (index,layero) {
                        var sale_area_edit = layer.getChildFrame('body', index).find("#sale_area_edit");
                        var new_data = {};
                        //把数组转为JSON
                        sale_area_edit.serializeArray().forEach(function(val){
                            new_data[val.name] = val.value;
                        });
                        console.log(new_data);
                        if(new_data["saleAreaName"]!=""){
                            $.post('sale-area/edit',new_data,function(info) {
                                layer.close(index);
                                layer.msg(info.msg);
                                tabInstance.reload();
                            })
                        }else {
                            layer.msg("请输入【区域名】！");
                        }
                    }
                });
                break;
            case 'del_btn':
                //删除
                layer.confirm('你确定要删除【'+obj.data.saleAreaName+'】吗？',{icon:3,anim: 4,skin:'layui-layer-molv'},function(){
                    $.post('sale-area/delete',{"saleAreaId":obj.data.saleAreaId},function(data){
                        tabInstance.reload();
                        layer.msg(data.msg);
                    });
                });
                break;
        }
    });

})